package hydrap2p.upload;

import hydrap2p.ServiceLocation;

import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;

public class UploadListenerThrift implements UploadListenerIface {

	private UploadListener.Client connection;
	private ServiceLocation location;
	
	public UploadListenerThrift(ServiceLocation location) throws TException
	{
		if (location==null)
			throw new TException("Invalid service location");
		
		this.location=location;

		TTransport transport = new TSocket(location.address,location.port);
		transport.open();
		connection=new UploadListener.Client(new TBinaryProtocol(transport));
		
		if (!connection.supportedInterfaces().contains("UploadListener"))
			throw new TException("Module does not implement UploadListener");
	}
	
	@Override
	public void newUpload(UploadQueued upload) throws Exception {
		connection.newUpload(upload);
	}

	@Override
	public void removedUpload(UploadQueued upload) throws Exception {
		connection.removedUpload(upload);
	}

	@Override
	public void updateUpload(UploadQueued upload) throws Exception {
		connection.updateUpload(upload);
	}
	
	public ServiceLocation getLocation()
	{
		return location;
	}

}
